package com.bstek.urule.console.servlet.respackage.sql;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Author hans
 * @CreateDate 2024-5-13
 */
public class MyJdbcService {

    @Autowired
    private ApplicationContext context;

    public Map<String, Object> getData(String sql) {
        Map<String, Object> mapdata = new HashMap<>();
        try {
            DataSource dataSource = (DataSource) context.getBean("mydatasource");
            JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
            List<Map<String, Object>> list = jdbcTemplate.queryForList(sql
                    , new Object[]{});
            System.out.println("从数据库中查询到的数据为=" + list);
            if (list.size() > 0) {
                mapdata = list.get(0);
            } else {
                System.out.println("MyJdbcService#getData：没有值啊。。。");
            }
        } catch (Exception ex) {

        }
        return mapdata;
    }

}
